package cn.edu.wynu.define;

import java.util.Scanner;

public class MethodTest04 {
    public static void main(String[] args) {
        // 案例一：计算1~n的和
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数：");
        int n = sc.nextInt();

        int sum1 = sum1(n);
        System.out.println("1" + "~" + n + "的和是：" + sum1);

        int sum2 = sum2(n);
        System.out.println("1" + "~" + n + "的和是：" + sum2);
    }
    // 方法一：循环
    public static int sum1(int n){
        int rs1 = 0;
        for (int i = 1; i <= n; i++) {
            rs1 += i;
        }
        return rs1;
    }
    // 方法二：递归，数学公式
    public static int sum2(int n){
        int rs2 = (1 + n) * n / 2;
        return rs2;
    }
}
